
/*
**
**	DON'T CHANGE ANYTHING HERE!
**	THIS SHOULD NOT BE USED TO LEARN FROM!
**	A BETTER SCRIPT TO LEARN FROM IS PEWPEW.LUA
**
*/

SWEP.PrintName = "Base"
SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl"
SWEP.Automatic = true
SWEP.AngleOffset = Angle( 0, 0, 0 )
SWEP.BuggyOffset = Vector( 2,-1.9,4 )
SWEP.Offset = Vector( 4,0,-1 )


function SWEP:Initialize()
end

function SWEP:Think()

end

function SWEP:Holster()
end

function SWEP:PrimaryAttack( targetEnt )
end

function SWEP:DrawHUD()

	local m_x, m_y = gui.MousePos()
	self:DrawCrosshair( m_x, m_y, 2 )

end

function SWEP:DrawWorldModel( pos, ang )

	self.Owner.WorldModel:DrawModel()
	self:DrawnWorldModel( pos, ang )

end

function SWEP:Create()

	local o = {}
	
	setmetatable( o, SWEPClasses[ self.ClassName ] )
	SWEPClasses[ self.ClassName ].__index = SWEPClasses[ self.ClassName ]
	o.fNextThink = 0
	o.fNextPrimaryAttack = 0
	o.fNextSecondaryAttack = 0
	
	return o
	
end

function SWEP:GetShootPos( )
	if CLIENT then
		return self.Owner:GetBuggyPos()+self.Owner:GetBuggyAngle():Up()*self.BuggyOffset.z+self.Owner:GetBuggyAngle():Right()*self.BuggyOffset.y + self.Owner:GetBuggyAngle():Forward()*self.BuggyOffset.x
	else
		return self.Owner:GetPos()+self.Owner:GetAngles():Up()*5+self.Owner:GetAngles():Right()*-5 + self.Owner:GetAngles():Forward()*4
	end
end

function SWEP:GetMuzzle()
	return self.Owner.muzzlePos or self:GetShootPos()
end

function SWEP:FireBullet( targetEnt, startPos, targetNormal )
	
	local pl = self.Owner
	
	if SERVER then
	
		--local pos = targetEnt:LocalToWorld(pl.InAttackVictim:OBBCenter()) - Vector(0,0,4)
		if ValidEntity( targetEnt ) then
			targetEnt:CarTakeDamage( 5 )
		end
		
		local pls = RecipientFilter()
		pls:AddAllPlayers()
		pls:RemovePlayer( pl )
		umsg.Start( "playerShot", pls )
			umsg.Entity( pl )
			umsg.Entity( targetEnt )
			umsg.Vector( startPos )
			umsg.Vector( targetNormal )
		umsg.End()
		
	else

		local scrNorm = ( GAMEMODE:GetFakeAngles():Forward()*-25 + pl:GetEyeTrace().HitPos - Vector(0,0,35) ) - EyePos()
		scrNorm:Normalize()
		local mx, my = gui.MousePos()
		
		local tracer_start = self:GetMuzzle()
		local tracer_end = EyePos() + ScreenToVector( mx, my ) * 1024 + Vector( math.random( -20,20 ), math.random( -20,20 ), math.random( -20,20 ) )
		if ValidEntity( targetEnt ) then
			-- If we have a target, lock onto them instead.
			tracer_end = targetEnt:LocalToWorld(targetEnt:OBBCenter()) - Vector(0,0,4) + Vector( math.random( -5,5 ), math.random( -5,5 ), math.random( -5,5 ) )
			targetEnt:SetBlinking( true )
			timer.Simple( 0.1, function() targetEnt:SetBlinking( false ) end )
		end
		local trRes = util.TraceLine( { start = tracer_start, endpos = tracer_end, filter = { pl, pl.Buggy } } )
		
		local TastyTracer = EffectData()
		TastyTracer:SetStart( tracer_start )
		TastyTracer:SetOrigin( trRes.HitPos )
		util.Effect( "tasty_tracer", TastyTracer )
		
	end
	
end

-- No secondary attack support :(

function SWEP:CanAttack()
	return CurTime() > self.fNextPrimaryAttack
end

function SWEP:NextAttack( time )
	self.fNextPrimaryAttack = time
end

if CLIENT then

function SWEP:FindTarget()

	m_x, m_y = gui.MousePos()

	to_aim_at = nil
	local trRes = util.TraceLine( { start = EyePos(), endpos = EyePos() + ScreenToVector( m_x, m_y )*1024, filter = self.Owner } )
	if trRes.Hit and ValidEntity( trRes.Entity ) then
		if trRes.Entity.Alive and !trRes.Entity:Alive() then return end
		if not ( trRes.Entity:IsNPC() or trRes.Entity:IsPlayer() or trRes.Entity:GetClass() == "prop_physics" ) then return end
		if trRes.Entity == self.Owner then return end
		to_aim_at = trRes.Entity
	end
	
end

function SWEP:GetAimVector()
	m_x, m_y = gui.MousePos()
	local trRes = util.TraceLine( { start = EyePos(), endpos = EyePos() + ScreenToVector( m_x, m_y )*1024, filter = self.Owner } )
	return ( trRes.HitPos - self:GetShootPos() ):GetNormal()
end

local CrossHairMat = CreateMaterial( "CrossHairMat", "UnlitGeneric", {
    ["$basetexture"] = "Sprites/crosshairs",
    ["$ignorez"] = "1",
    ["$vertexcolor"] = "1",
    ["$vertexalpha"] = "1",
    ["$nolod"] = "1",
} )

local cross_x, cross_y = 0,0
local cross_w, cross_h = 66,66
local s_x, s_y = 25,49
local s_w, s_h = 21,21
local crosshair_tab = {
{ x=cross_x, y=cross_y, u=s_x/128, v=s_y/128 },
{ x=cross_x+cross_w, y=cross_y, u=(s_x+s_w)/128, v=s_y/128 },
{ x=cross_x+cross_w, y=cross_y+cross_h, u=(s_x+s_w)/128, v=(s_y+s_h)/128 },
{ x=cross_x, y=cross_y+cross_h, u=s_x/128, v=(s_y+s_h)/128 }
}


function draw_crosshair( x, y, time )
	local dist = Vector( cross_w, cross_h, 0 ):Length2D()/2

	crosshair_tab[1].x = x + dist*math.cos( time )
	crosshair_tab[2].x = x + dist*math.cos( time+(2/4*math.pi) )
	crosshair_tab[3].x = x + dist*math.cos( time+(4/4*math.pi) )
	crosshair_tab[4].x = x + dist*math.cos( time+(6/4*math.pi) )

	crosshair_tab[1].y = y + dist*math.sin( time )
	crosshair_tab[2].y = y + dist*math.sin( time+(2/4*math.pi) )
	crosshair_tab[3].y = y + dist*math.sin( time+(4/4*math.pi) )
	crosshair_tab[4].y = y + dist*math.sin( time+(6/4*math.pi) )
	
	surface.DrawPoly( crosshair_tab )
end

function SWEP:DrawCrosshair( x, y, speed )
	local time = CurTime() * 2 * speed
	surface.SetMaterial( CrossHairMat )
	surface.SetDrawColor( 0,0,0,255 )
	draw_crosshair( x+2,y+2,time )
	draw_crosshair( x,y,time )
	draw_crosshair( x,y+2,time )
	draw_crosshair( x+2,y,time )
	surface.SetDrawColor( 255,255,255,255 )
	draw_crosshair( x+1,y+1,time )

	if ValidEntity( to_aim_at ) then
		local obj = (to_aim_at:LocalToWorld(to_aim_at:OBBCenter()) - Vector(0,0,4)):ToScreen()
		surface.SetDrawColor( 255,0,0,255 )
		draw_crosshair( x+1,y+1, time )
	end

end

function SWEP:GetWorldModel()
	return self.Owner.WorldModel
end

function SWEP:GetDrawPos()

	local ang
	if LocalPlayer() == self.Owner then
		local mx, my = gui.MousePos()
		ang = ScreenToVector( mx, my ):Angle()
	else
		ang = self.Owner:GetAimVector():Angle() -- Network their mouse position.
	end
	
	ang:RotateAroundAxis( ang:Right(), self.AngleOffset.p )
	ang:RotateAroundAxis( ang:Up(), self.AngleOffset.y )
	ang:RotateAroundAxis( ang:Forward(), self.AngleOffset.r )
	
	return self:GetShootPos() - ang:Up() * self.Offset.z + ang:Forward() * self.Offset.x + ang:Right() * self.Offset.y, ang
end

function SWEP:DrawnWorldModel( pos, ang )
end

end
